<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyValue" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search()">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.shops" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="姓名"  sortable>
			</el-table-column>
			<el-table-column prop="tel" label="电话"  sortable>
			</el-table-column>
			<el-table-column prop="registerTime" label="注册时间"  sortable>
			</el-table-column>
			<el-table-column prop="address" label="商店地址"  sortable>
			</el-table-column>
			<el-table-column prop="logo" label="商店图片" sortable>
				<template slot-scope="scope" >
					<img :src="scope.row.logo" style="width: 100px; height: 100px"/>
				</template>
			</el-table-column>
			<el-table-column prop="admin.realName" label="管理员"  sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" sortable>
				<template slot-scope="scope">
					<span v-if="scope.row.state==1" style="color: yellow">待审核</span>
					<span v-if="scope.row.state==2" style="color: #ff4949">待激活</span>
					<span v-if="scope.row.state==3" style="color: #1d8ce0">激活成功</span>
					<span v-if="scope.row.state==4" style="color: #8c939d">驳回</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" fixed="right" width="280px">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
					<el-button type="primary" size="small" v-if="scope.row.state==1" @click="manualAudit(scope.$index, scope.row)">手动审核</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
						   :current-page="query.currentPage" :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="姓名" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话" prop="name">
					<el-input v-model="saveForm.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="地址" prop="sn">
					<el-input v-model="saveForm.address" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="管理员" prop="url">
					<el-select v-model="saveForm.admin.id" placeholder="请选择">
						<el-option
								v-for="item in adminTree"
								:key="item.id"
								:label="item.realName"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="封面">
					<el-upload
							class="upload-demo"
							action="http://localhost:9090/fastDfs/upload"
							:on-remove="coverHandleRemove"
							:on-success="coverHandleSuccess"
							:file-list="coverFileList"
							list-type="picture">
						<el-button size="small" type="primary">点击上传</el-button>
						<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
					</el-upload>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

<!--		手动审核-->
		<el-dialog title="手动审核" :visible.sync="manualAuditVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="姓名" prop="name" >
					<el-input v-model="saveForm.name"  auto-complete="off" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item label="地址" prop="sn">
					<el-input v-model="saveForm.address" auto-complete="off" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item label="电话" prop="sn">
					<el-input v-model="saveForm.tel" auto-complete="off" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item label="管理员" prop="url">
					<el-select v-model="saveForm.admin.id" placeholder="请选择" :disabled="true">
						<el-option
								v-for="item in adminTree"
								:key="item.id"
								:label="item.realName"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="图片" prop="sn">
					<el-image :src="saveForm.logo" label="封面" style="width: 50px;height: 50px" :preview-src-list="srcList">
					</el-image>
				</el-form-item>
				<el-form-item label="审核描述">
					<el-input type="textarea" v-model="saveForm.info"></el-input>
				</el-form-item>

			</el-form>

			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="shopAuditSubmitSuccess" :loading="addLoading">审核通过</el-button>
				<el-button type="primary" @click.native="shopAuditSubmitReject" :loading="addLoading">审核失败</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	// import util from '../../common/js/util'
	// //import NProgress from 'nprogress'
	// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {

			return {
				query:{
					currentPage:1,
					pageSize:10,
					keyValue:""
				},
				shops: [],
				checkAll:[],
				srcList: [],
				coverFileList:[],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列
				setPermissionVisible:false,//编辑用户权限页面
				isIndeterminate:false,
				editFormVisible: false,//编辑界面是否显示
				manualAuditVisible:false,//手动审核页面是否开启
				editLoading: false,
				saveLoading:false,
				query:{
					currentPage:1,
					pageSize:5,
					keyValue:''
				},
				pageInfo: {
					total:0,
					shops:[],
				},
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
					sn: [
						{ required: true, message: '请输入标识', trigger: 'blur' }
					]
				},
				//编辑界面数据
				saveFormVisible: false,//新增界面是否显示
				addLoading: false,

				saveFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
					sn: [
						{ required: true, message: '请输入标识', trigger: 'blur' }
					]
				},
				//新增界面数据
				saveForm: {
					id:null,
					name: "",
					tel:0,
					address:"",
					// logo:"",
					admin:{
						id:null,
						name:""
					}
				},
				moreimgFileList:[],
				adminTree:[]
			}

		},
		methods: {
			coverHandleRemove(file, fileList){
				var filePath = file.response.object;
				this.$http.delete("/fastDfs/delete?path="+filePath)
						.then(result => {
							result = result.data;
							if(result.success){
								this.$message({message: '删除成功!', type: 'success'});
								this.carForm.cover = "";
							}else{
								this.$message({message: result.message, type: 'error'});
							}
						})
			},
			coverHandleSuccess(response, file, fileList){
				this.carForm.cover = response.object;
			},
			manualAudit(index,row){
				this.manualAuditVisible=true;
				this.saveForm=row;
				this.srcList.push(this.saveForm.logo);
			},
			search(){
				this.query.currentPage=1;
				this.getShops();
			},
			handleCurrentChange(val) {
				this.query.currentPage = val;
				this.getShops();
			},
			//获取用户列表
			getShops() {
				this.$http.post("/shop",this.query)
						.then(result=>{
							result=result.data;
							if (result.success){
								this.pageInfo.total=result.object.size;
								this.pageInfo.shops=result.object.rows;
							}else {
								this.$message({
									message:result.message,
									type:'error'
								});
							}

						})
				this.getAdminTree();
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/shop/"+row.id).then(result=>{
						this.listLoading=false;
						result=result.data;
						if (result.success){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.query.currentPage=1;
							this.getShops();
						}
					})
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);

			},
			//显示新增界面
			handleAdd: function () {
				this.saveFormVisible = true;
				this.saveForm = {
					// id:null,
					name: "",
					title:""
				};

			},
			getAdminTree(){
				this.$http.get("/employee").then(result=>{
					result=result.data;
					this.adminTree=result.object;
					console.log(this.adminTree)
				})
			},
			shopAuditSubmitSuccess(){
				this.saveForm.state=2;
				console.log(this.saveForm)
				this.$http.post("/shop/settlementByManual",this.saveForm).then(result=>{
					result=result.data;
					if (result.success){
						this.manualAuditVisible=false;
						this.$message({
							message: '审核成功',
							type: 'success'
						});
					}else {
						this.$message({
							message: '审核失败'+result.message,
							type: 'error'
						});
					}
				})
			},
			shopAuditSubmitReject(){
				this.saveForm.state=4;
				this.$http.post("/shop/settlementByManual",this.saveForm).then(result=>{
					result=result.data;
					if (result.success){
						this.manualAuditVisible=false;
						this.$message({
							message: '审核成功',
							type: 'success'
						});
					}else {
						this.$message({
							message: '审核失败'+result.message,
							type: 'error'
						});
					}
				})
			},
			//新增
			addSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							this.saveFormVisible=true;
							//NProgress.start();
							let para=Object.assign({}, this.saveForm);
							console.log(para)
							this.$http.put("/shop",this.saveForm).then(result=>{
								this.addLoading=false;
								result=result.data;
								console.log(result," shop")
								if (result.success){
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.saveFormVisible=false;
									this.getShops();
								}else {
									this.$message({
										message: '提交失败',
										type: 'error'
									});
								}
							}).catch(()=>{
								this.$message({
									message: result.message,
									type: 'error'
								});
							})
						});
					}
				});
			},
			selsChange(sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					console.log(ids)
					this.$http.patch("/shop",ids).then(result=>{
						this.listLoading = false;
						result=result.data;
						console.log(result," 313113");
						if (result.success){
							this.$message({
								message: '批量删除成功',
								type: 'success'
							});
							this.query.currentPage=1;
							this.getShops();
						}else{
							this.$message({
								message: '批量删除失败'+result.message,
								type: 'error'
							});
						}
					})

				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getShops();
		}
	}

</script>

<style scoped>

</style>